/*
 * 文件名：NetworkPackage.java
 * 版权：亚信科技(南京)版权所有
 * 描述：TODO
 * 修改人：xuwei3
 * 修改时间：2015-11-11
 * 修改内容：TODO
 */
package com.ailk.bigdata.etl.realstream.server.model;

import io.netty.buffer.ByteBuf;

import java.io.Serializable;
import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ailk.bigdata.etl.common.server.constant.Constant;
import com.ailk.bigdata.etl.common.server.net.ByteUtil;
import com.ailk.bigdata.etl.realstream.server.service.FormatHandler;
import com.ailk.bigdata.etl.realstream.server.tools.RealStreamTool;

/**
 * @description TODO
 * @author [xuwei3]
 * @version [版本号,2015-11-11]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class BaseNetPackage implements Serializable
{
	private static final long serialVersionUID = -7288120617598608541L;
	private static Logger logger = LoggerFactory.getLogger(BaseNetPackage.class);

	private int dataType;
	private long cdrId;// sdtp协议批次号
	private ByteBuf dataContent;
	private String sendContent;
	private String separator;

	public int getDataType()
	{
		return dataType;
	}

	public void setDataType(int dataType)
	{
		this.dataType = dataType;
	}

	public long getCdrId()
	{
		return cdrId;
	}

	public void setCdrId(long cdrId)
	{
		this.cdrId = cdrId;
	}

	public ByteBuf getDataContent()
	{
		return dataContent;
	}

	public void setDataContent(ByteBuf dataContent)
	{
		this.dataContent = dataContent;
	}

	public String getSendContent()
	{
		return sendContent;
	}

	public void setSendContent(String sendContent)
	{
		this.sendContent = sendContent;
	}

	public String getSeparator()
	{
		return separator;
	}

	public void setSeparator(String separator)
	{
		this.separator = separator;
	}

	/**
	 * 
	 * @description 转换数据报格式
	 * @param acceptformat
	 * @param sendformat
	 * @throws Exception
	 */
	public void format(long onlyCode, RealStreamFormat acceptformat, RealStreamFormat sendformat) throws Exception
	{
		StringBuilder sbuilder = new StringBuilder();

		setSeparator(sendformat.getDataFormatDefine());
		
		if (this instanceof LTENetPackage)
		{
			LTENetPackage lte = (LTENetPackage) this;
			sbuilder.append(lte.getLtePublic().toString(sendformat.getDataFormatDefine()));
		}

		if (Constant.FILE_ASC_TYPE == acceptformat.getDataFormatType())
		{
			FormatHandler formatHandler = RealStreamTool.DATA_FORMAT_HANDLER_MAP.get(onlyCode);
			formatHandler.handleRequest(getDataContent(), sbuilder);
		}
		else
		{
			String content = new String(getDataContent().array(), "utf-8").trim();
			// 分隔符
			if (Constant.FILE_ASC_TYPE == sendformat.getDataFormatType())
			{
				String[] dataFormats = StringUtils.split(sendformat.getDataFormatDefine(), ";");
				String[] dataContents = StringUtils.split(content, acceptformat.getDataFormatDefine());
				int count = 0;
				for (String dataContent : dataContents)
				{
					int length = Integer.parseInt(StringUtils.split(dataFormats[count++], "=")[1]);
					int flag = dataContent.length();
					while (flag++ < length)
					{
						sbuilder.append(" ");
					}
					sbuilder.append(dataContent);
				}
			}
			else
			{
				sbuilder.append(content);
			}
		}

		if (logger.isDebugEnabled())
		{
			logger.debug("dataType[" + getDataType() + "]length[" + getDataContent().array().length + "]字节码["
					+ Arrays.toString(getDataContent().array()) + "]16进制码[" + ByteUtil.toHexString(getDataContent().array()) + "]解析后内容["
					+ sbuilder.toString() + "]");
		}
		this.setDataContent(null);
		this.setSendContent(sbuilder.toString());
		sbuilder = null;
	}

	public static void main(String[] args)
	{
		System.out.println(Integer.parseInt("F", 16));
	}

}
